package org.mcjug.locationmailer.activity;

import java.text.DateFormat;
import java.util.Date;

import org.mcjug.locationmailer.R;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.widget.TextView;
import android.widget.SimpleCursorAdapter.ViewBinder;

public class LocationListViewBinder implements ViewBinder {

	@Override
	public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
		Context context = view.getContext();
		CharSequence textStr = null;
		if (view.getId() == R.id.textCreatedDate) {
			long timestamp = cursor.getLong(columnIndex);
			DateFormat format = DateFormat.getDateTimeInstance(
					DateFormat.MEDIUM, DateFormat.SHORT);
			CharSequence time = format.format(new Date(timestamp));
			textStr = time;

		} else if (view.getId() == R.id.textLatitude) {
			String latitude = cursor.getString(columnIndex);
			if (latitude == null) {
				textStr = context.getString(R.string.msgNoLatitude);
			} else {
				textStr = String.format(context.getString(R.string.latitudeFormatted), Double.parseDouble(latitude));
			}
		} else if (view.getId() == R.id.textLongitude) {
			String longitude = cursor.getString(columnIndex);
			if (longitude == null) {
				textStr = context.getString(R.string.msgNoLongitude);
			} else {
				textStr = String.format(context.getString(R.string.longitudeFormatted), Double.parseDouble(longitude));
			}
		}

		if (textStr != null) {
			((TextView) view).setText(textStr);
			return true;
		} else {
			return false;
		}
	}
}
